package com.rpframework.website.device.act.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Dynamic;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.DynamicService;

@Controller
@RequestMapping("api/dynamic")
public class ApiDynamicAct extends AdminAct{
	@Resource DynamicService dynamicService;
	Gson gson=new Gson();
	
	@RequestMapping(value="/list" , produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager" ,required=false)Pager<Dynamic> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			Map<Object, Object> model, RedirectAttributes attr,HttpSession session){
		if(pager==null){
			pager=new Pager<Dynamic>();
		}
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}	
		pager.getSearchMap().put("category", String.valueOf(user.getEstateId()));
		pager.getSearchMap().put("fenzhu", "fenzhu");
		pager=dynamicService.getpager(pager);
		List<Dynamic> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for(Dynamic act:list){
			List<Dynamic> listdy=dynamicService.selectBytime(act.getDatime(),act.getPropertyid());
			act.setListdynamic(listdy);
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			
			array.add(jsonObj);
		}
		
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;

	}
	//活动 动态
		 @RequestMapping(value="/listone" ,produces = "application/json; charset=utf-8")
			 public @ResponseBody String listone(@RequestParam String ynamicid,
					 @RequestParam(value = "callback",required = false ) String callback,
					 HttpSession session){
			 Dynamic wind=dynamicService.selectOnlyOne(Integer.parseInt(ynamicid));
				JsonObject json=new JsonObject();
				json.addProperty("title", wind.getTitle());
				json.addProperty("onepic", wind.getOnepic());
				json.addProperty("msg", wind.getMsg());
			
				json.addProperty("starttime", TagUtils.formatDate(wind.getStarttime()));
				if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
				return ""+json ;
			}
}
